<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<jk-add-button :permission-prefix="prefix" class="margin-5-0 item-gap" @click="onAddBtn"></jk-add-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<vxe-column
					v-for="(item, index) in tableColumnList"
					:key="index"
					:title="item.title"
					:align="item.align"
					:field="item.keyName"
					:sortable="item.sortable"
					:fixed="item.fixed ? item.fixed : ''"
					:min-width="item.minWidth"
					:width="item.width"
				>
					<template slot-scope="scope">
						<div v-if="item.keyName === 'electricityPriceList'">
							<div v-for="(i,ids) in scope.row.electricityPriceList" :key="ids">
								<div>
									<span>{{ i.typeName }}</span>
									<span style="margin: 0 2rem">{{ i.startTime }} - {{ i.endTime }}</span>
									<span>单价{{ i.price }}</span>
								</div>
							</div>
						</div>
						<div v-else>{{ scope.row[item.keyName] }}</div>

					</template>
				</vxe-column>
				<vxe-column title="操作" align="center" fixed="right" :width="250">
					<template #default="scope">
						<jk-operation-bar
							:scope="scope"
							api-prefix="machinemanager/electricity-price-main"
							:api-item="apiItem"
							:permission-prefix="prefix"
							@on-detail="onDetailBtn"
							@on-delete="onDeleteBtn"
							@on-edit="onEditBtn"
							@on-refresh="getListRequest"
						></jk-operation-bar>
					</template>

				</vxe-column>

			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<save-dialog
			:page-type="pageType"
			:data-id="dataId"
			:dialog-state="saveDialogState"
			@on-visible-change="onVisibleChange"
			@on-finish="onFinish"
		></save-dialog>
	</div>
</template>

<script>
    import saveDialog from './save-dialog';
    import calHeight from '@/mixins/cal-table-height';
    import { pageConfig, tipsBar } from '@/utils/jk/jk-tool';
    import {
        delElector,
        getElectorList
    } from '@/api/energy/elector';
    import getMenuColumn from '@/mixins/getMenuColumn';
    import { spinUpkeepItemDelete, spinUpkeepItemExportExcel, spinUpkeepItemPage } from '@/api/basicData/upkeepItem';
    export default {
        name: 'Elector',
        components: {
            saveDialog
        },
        mixins: [calHeight, getMenuColumn],
        data() {
            return {
                apiItem: {
                    'page': getElectorList,
                    'delete': delElector
                },
                pageType: '',
                // 按钮权限标识符
                prefix: 'machinemanager:electricityPriceMain',
                dataId: null,
                saveDialogState: false,
                tableLoading: true,
                tableData: [],
                totalCount: 0,
                processList: [],
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    maxValue: null,
                    auditState: null
                }
            };
        },
        mounted() {
            this.getDependentData();
        },
        methods: {
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            async getDependentData() {
                this.tableLoading = true;
                await this.getMenuColumnListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            },

            onFinish() {
                this.saveDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                this.saveDialogState = e;
                if (!e) {
                    this.dataId = null;
                }
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                return this.apiItem['page'](this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 新增按钮操作 */
            onAddBtn() {
                this.pageType = 'add';
                this.saveDialogState = true;
            },

            /** 删除按钮操作 */
            onDeleteBtn(row) {
                this.apiItem['delete']({ id: row.id }).then((res) => {
                    if (res.data.status === 200) {
                        this.getListRequest();
                        this.$tipsBar(this, 'delete');
                    }
                }).catch(() => {});
            },

            /** 修改按钮操作 */
            onEditBtn(row) {
                this.pageType = 'edit';
                this.dataId = row.id;
                this.saveDialogState = true;
            },
            onDetailBtn(row) {
                this.pageType = 'detail';
                this.dataId = row.id;
                this.saveDialogState = true;
            }

        }
    };
</script>
